package com.example.springboot.test;

import com.alibaba.fastjson.JSONObject;
import com.example.springboot.request.UserLoginRequest;
import com.example.springboot.request.UserQueryRequest;
import com.example.springboot.request.UserResetRequest;
import com.example.springboot.request.UserSaveRequest;
import com.example.springboot.response.CommonResponse;
import com.example.springboot.response.UserLoginResponse;
import com.example.springboot.response.UserQueryResponse;
import com.example.springboot.response.PageResponse;
import com.example.springboot.service.ServiceUser;
import com.example.springboot.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;


@RestController//表示访问这个地址时要返回一个字符串
public class UserController {

    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Resource
    private ServiceUser serviceUser;

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private RedisTemplate redisTemplate;

    //查询
    @GetMapping("/User/service")
    public CommonResponse list(@Valid UserQueryRequest userQueryRequest){
        CommonResponse<PageResponse<UserQueryResponse>> response=new CommonResponse<>();
       PageResponse<UserQueryResponse> list= serviceUser.list(userQueryRequest);
        response.setContent(list);
        return response;
    }

    //保存
    @PostMapping("/User/save")
    public CommonResponse save(@Valid @RequestBody UserSaveRequest userSaveRequest){
        userSaveRequest.setPassword(DigestUtils.md5DigestAsHex(userSaveRequest.getPassword().getBytes()));

        CommonResponse response=new CommonResponse<>();
        serviceUser.save(userSaveRequest);
        return response;
    }

    //重置密码
    @PostMapping("/User/reset-password")
    public CommonResponse resetPassword(@Valid @RequestBody UserResetRequest userResetRequest){
        //对密码进行加密
        userResetRequest.setPassword(DigestUtils.md5DigestAsHex(userResetRequest.getPassword().getBytes()));

        CommonResponse response=new CommonResponse<>();
        serviceUser.resetPassword(userResetRequest);
        return response;
    }

    //登录
    @PostMapping("/User/login")
    public CommonResponse login(@Valid @RequestBody UserLoginRequest userLoginRequest){

        //登录也同样需要两层加密（前后端均加密），这样我们才能拿加密后的密文和数据库中的对比
        userLoginRequest.setPassword(DigestUtils.md5DigestAsHex(userLoginRequest.getPassword().getBytes()));
        CommonResponse<UserLoginResponse> response=new CommonResponse<>();
        UserLoginResponse userLoginResponse= serviceUser.login(userLoginRequest);
        Long token = snowFlake.nextId();
        LOG.info("生成单点登录token：{}，并放入redis中",token);
        userLoginResponse.setToken(token.toString());
        //不能直接把userLoginResponse放在这，得通过JSON序列化
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(userLoginResponse),3600 * 24, TimeUnit.SECONDS);

        response.setContent(userLoginResponse);
        return response;
    }

    //删除
    @DeleteMapping("/User/delete/{id}")
    public CommonResponse delete(@PathVariable Long id){
        CommonResponse response=new CommonResponse<>();
        serviceUser.delete(id);
        return response;
    }

    //退出登录
    @GetMapping("/User/logout/{token}")
    public CommonResponse logout(@PathVariable String token) {
        CommonResponse response = new CommonResponse<>();
        redisTemplate.delete(token);
        LOG.info("从redis中删除token：{}",token);
        return response;
    }
}
